<?php
class Site_global {
   /**
   * 用于普通的分页,跳转模式
   *
   * @param array()       $par
   * @param int           $total
   * @param int           $page
   * @param int           $size
   * @return string
   */
  static function listPage($par, $total, $page, $size, &$sortUrl = "") {
    $par_string = "";
    $sort_string = "";
    foreach ( $par as $key => $val ) {
      if (trim ( $val ) == "")
      continue;
      $par_string .= "&" . trim ( $key ) . "=" . trim ( $val );

      //给排序用的
      if ($key == "order_key" || $key == "order_meth")
      continue;
      $sort_string .= "&" . trim ( $key ) . "=" . trim ( $val );
    }
    $link = $_SERVER ['PHP_SELF'] . "?ctl=" . $_REQUEST ['ctl'] . "&act=" . $_REQUEST ['act'] . $par_string;
    $sortUrl = $_SERVER ['PHP_SELF'] . "?ctl=" . $_REQUEST ['ctl'] . "&act=" . $_REQUEST ['act'] . $sort_string . "&p=" . $page;

    /* 第一页 */
    $first = ($page == 1) ? "<span id='page_first' class='page_cur_num'>第一页</span>" : "<span id='page_first' class='page_num'><a href='" . $link . "&p=1'>第一页</a></span>";
    /* 最后一页 */
    if (ceil ( $total / $size ) == 1) {
      $last = "";
    } else {
      $last = ceil ( $total / $size ) <= $page ? "<span id='page_last' class='page_cur_num'>末页</span>" : "<span id='page_last' class='page_num'><a href='" . $link . "&p=" . ceil ( $total / $size ) . "'>末页</a></span>";
    }

    $pre = $page > 1 ? "<span id='pre_page'><a href='" . $link . "&p=" . ($page - 1) . "'>上一页</a></span>" : "<span id='pre_page'>上一页</span>";
    $next = $page < ceil ( $total / $size ) ? "<span id='pre_page'><a href='" . $link . "&p=" . ($page + 1) . "'>下一页</a></span>" : "<span id='pre_page'>下一页</span>";

    /* 中间部份 */
    $middle = "";
    $front = $page - 4;
    $after = $page + 4;
    for($i = $front; $i <= $after; $i ++) {

      if ($i <= 1 || $i >= ceil ( $total / $size )) {
        continue;
      }
      $middle .= "&nbsp;<a href='" . $link . "&p=" . $i . "'";
      $middle .= ($page == $i) ? "style='color:red;font-weight:bold;' class='page_cur_num'" : " class='page_num'";
      $middle .= ">" . $i . "</a>";
    }

    $goto = "";
    $goto .= "<span id='page_goto'>转到";
    $goto .= "<input type='text' id='goto_page' value='" . $page . "' onfocus='this.select();'" . " size='2' onblur=\"window.location.href='" . $link . "&p='+this.value+''\" />页";
    $goto .= "<input type='button' value='go'" . " onclick=\"window.location.href='" . $link . "&p='+document.getElementById('goto_page').value+''\" />";

    return "<span id='page_total'>共取得" . $total . "条数据，合计" . ceil ( $total / $size ) . "页。&nbsp;" . " - " . $first . "&nbsp;" . $pre . "&nbsp;" . $middle . "&nbsp;" . $next . "&nbsp;" . $last . "&nbsp;";
    //.$goto;
  }
}

?>
